package com.example.lj.uav.view;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.lj.uav.MyApplication;
import com.example.lj.uav.R;
import com.example.lj.uav.adapter.SpinnerDropDownAdapter;
import com.example.lj.uav.enums.ButtonActionCN;
import com.example.lj.uav.utils.OnUIUtils;
import com.example.lj.uav.view.base.BaseActivity;
import com.example.lj.uav.widget.CommomDialog;


import java.util.Map;

import dji.common.error.DJIError;
import dji.common.remotecontroller.ProfessionalRC;
import dji.common.util.CommonCallbacks;
import dji.sdk.remotecontroller.RemoteController;

public class ControllerSettingActivity extends BaseActivity implements View.OnClickListener,
        RadioGroup.OnCheckedChangeListener, AdapterView.OnItemClickListener {

    private Context context;
    private RadioGroup rg_front_back;
    private RemoteController remoteController;
    private Spinner sp_user;
    private SpinnerDropDownAdapter userAdapter;
    private TextView BB, BA, TD, BD, BC, BE, BF, RS, C4, C2, BG, BH, LS, C3, C1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_controller_setting);
        context = this;

        findViewById(R.id.controller_v_finish).setOnClickListener(this);
        findViewById(R.id.controller_v_setting).setOnClickListener(this);
        findViewById(R.id.controller_tv_reset).setOnClickListener(this);

        rg_front_back = findViewById(R.id.controller_rg_front_back);
        rg_front_back.setOnCheckedChangeListener(this);

        remoteController = MyApplication.getRemoteControllerInstance();
        if (remoteController == null) {
            showToast("未检测到设备");
            return;
        }
        if (remoteController.isCustomizableButtonSupported()) {
            showToast("设备不支持该设置");
            return;
        }

        sp_user = findViewById(R.id.controller_sp_user);
        sp_user.setOnItemClickListener(this);
        getUserName();
        initCustomButton();
    }

    /**
     * 自定义按键
     */
    private void initCustomButton() {
        BA = findViewById(R.id.controller_tv_ba);
        BB = findViewById(R.id.controller_tv_bb);
        BC = findViewById(R.id.controller_tv_bc);
        BD = findViewById(R.id.controller_tv_bd);
        BE = findViewById(R.id.controller_tv_be);
        BF = findViewById(R.id.controller_tv_bf);
        BG = findViewById(R.id.controller_tv_bg);
        BH = findViewById(R.id.controller_tv_bh);
        C1 = findViewById(R.id.controller_tv_c1);
        C2 = findViewById(R.id.controller_tv_c2);
        C3 = findViewById(R.id.controller_tv_c3);
        C4 = findViewById(R.id.controller_tv_c4);
        TD = findViewById(R.id.controller_tv_td);
        RS = findViewById(R.id.controller_tv_rs);
        LS = findViewById(R.id.controller_tv_ls);
    }

    /**
     * 获取可自定义按钮配置文件组的名称
     */
    private void getUserName() {
        remoteController.getButtonProfileGroups(new CommonCallbacks.CompletionCallbackWith<String[]>() {
            @Override
            public void onSuccess(String[] strings) {
                userAdapter = new SpinnerDropDownAdapter(context, strings, sp_user);
                sp_user.setAdapter(userAdapter);
            }

            @Override
            public void onFailure(DJIError djiError) {
                if (djiError != null) {
                    OnUIUtils.setToast(djiError.getDescription());
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.controller_v_finish:
                finish();
                break;
            case R.id.controller_v_setting:

                break;
            case R.id.controller_tv_reset:
                new CommomDialog(context, "确认要重置按键吗", new CommomDialog.OnCloseListener() {
                    @Override
                    public void onClick(CommomDialog dialog, boolean confirm) {
                        if (confirm) {
                            remoteController.resetButtonConfig(new CommonCallbacks.CompletionCallback() {
                                @Override
                                public void onResult(DJIError djiError) {
                                    OnUIUtils.setToast(djiError == null ? "重置成功" : "重置失败");
                                }
                            });
                        }
                        dialog.dismiss();
                    }
                }).show();
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        //正面 背面
        if (group == rg_front_back) {
            if (checkedId == R.id.controller_rb_front) {
                findViewById(R.id.controller_v_front_point).setBackgroundResource(R.drawable.ic_point_blue);
                ((RadioButton) findViewById(R.id.controller_rb_front)).setTextColor(ContextCompat.getColor(this, R.color.dodgerblue));
                findViewById(R.id.controller_v_back_point).setBackgroundResource(R.drawable.ic_point_gray);
                ((RadioButton) findViewById(R.id.controller_rb_back)).setTextColor(ContextCompat.getColor(this, R.color.gray_light));
                findViewById(R.id.controller_rl_back).setVisibility(View.GONE);
                findViewById(R.id.controller_rl_front).setVisibility(View.VISIBLE);
            } else if (checkedId == R.id.controller_rb_back) {
                findViewById(R.id.controller_v_front_point).setBackgroundResource(R.drawable.ic_point_gray);
                ((RadioButton) findViewById(R.id.controller_rb_front)).setTextColor(ContextCompat.getColor(this, R.color.gray_light));
                findViewById(R.id.controller_v_back_point).setBackgroundResource(R.drawable.ic_point_blue);
                ((RadioButton) findViewById(R.id.controller_rb_back)).setTextColor(ContextCompat.getColor(this, R.color.dodgerblue));
                findViewById(R.id.controller_rl_front).setVisibility(View.GONE);
                findViewById(R.id.controller_rl_back).setVisibility(View.VISIBLE);
            }

        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (view.getId()) {
            case R.id.controller_sp_user:
                userAdapter.getArray();
                break;
        }
    }

    private void getButtonConfig() {
        remoteController.getButtonConfig(new CommonCallbacks.CompletionCallbackWith<ProfessionalRC.ButtonConfiguration>() {
            @Override
            public void onSuccess(ProfessionalRC.ButtonConfiguration buttonConfiguration) {
                Map<ProfessionalRC.CustomizableButton, ProfessionalRC.ButtonAction> buttonActions = buttonConfiguration.getButtonActions();
//                buttonActions.get(ProfessionalRC.CustomizableButton.BA)
            }


            @Override
            public void onFailure(DJIError djiError) {
                if (djiError != null)
                    OnUIUtils.setToast(djiError.getDescription());
            }
        });
    }
}
